using System.Collections.Generic;
using UnityEngine;
using com.adjust.sdk;

namespace Voodoo.Sauce.Internal.Analytics
{
	public static class AdjustWrapper
	{
		private const string TAG = "AdjustWrapper";

		private const string ADJUST_USER_ID_KEY = "vs_adjust_user_id";

		private static readonly AttributionData AttributionData = new AttributionData
		{
			Name = "Adjust"
		};

		private static bool _isInitialized;

		private static readonly Queue<AdjustAnalyticsEvent> EventsQueue = new Queue<AdjustAnalyticsEvent>();

		internal static void Instantiate()
		{
			NintendoPlayerPrefs.DeleteKey("vs_adjust_user_id");
			VoodooLog.LogDebug(Module.ANALYTICS, "AdjustWrapper", "Adjust started");
		}

		public static void Initialize(string token, bool isTestMode, bool isChinaBuild)
		{
			AddAdjustSessionCallbackParameters();
			InitializeAdjust(token, isTestMode, isChinaBuild);
			_isInitialized = true;
			while (EventsQueue.Count > 0)
			{
				EventsQueue.Dequeue().Track();
			}
		}

		public static void TrackEvent(string eventName, Dictionary<string, object> customVariables = null)
		{
			if (!string.IsNullOrEmpty(eventName))
			{
				AdjustAnalyticsEvent adjustAnalyticsEvent = new AdjustAnalyticsEvent(eventName, customVariables);
				if (!_isInitialized)
				{
					VoodooLog.LogDebug(Module.ANALYTICS, "AdjustWrapper", "Adjust NOT initialized queuing event..." + eventName);
					EventsQueue.Enqueue(adjustAnalyticsEvent);
				}
				else
				{
					VoodooLog.LogDebug(Module.ANALYTICS, "AdjustWrapper", "Sending event " + eventName + " to Adjust");
					adjustAnalyticsEvent.Track();
				}
			}
		}

		public static void TrackAdRevenue(string eventName, string data)
		{
			if (!string.IsNullOrEmpty(eventName))
			{
				if (!_isInitialized)
				{
					VoodooLog.LogDebug(Module.ANALYTICS, "AdjustWrapper", "Adjust NOT initialized ignore Ad revenue event..." + eventName);
					return;
				}
				VoodooLog.LogDebug(Module.ANALYTICS, "AdjustWrapper", "Sending AdRevenue " + eventName + "  to Adjust");
				Adjust.trackAdRevenue(eventName, data);
			}
		}

		public static void TrackAdRevenue(AdjustAdRevenue adRevenue)
		{
			if (adRevenue != null)
			{
				if (!_isInitialized)
				{
					VoodooLog.LogDebug(Module.ANALYTICS, "AdjustWrapper", "Adjust NOT initialized ignore Ad revenue event..." + adRevenue.source);
					return;
				}
				VoodooLog.LogDebug(Module.ANALYTICS, "AdjustWrapper", "Sending AdRevenue " + adRevenue.source + "  to Adjust");
				Adjust.trackAdRevenue(adRevenue);
			}
		}

		private static void AddAdjustSessionCallbackParameters()
		{
		}

		private static void AddAdjustSessionCallbackParameters(Dictionary<string, string> parameters)
		{
			if (parameters == null)
			{
				return;
			}
			foreach (KeyValuePair<string, string> parameter in parameters)
			{
				Adjust.addSessionCallbackParameter(parameter.Key, parameter.Value);
			}
		}

		private static void InitializeAdjust(string adjustAppToken, bool isTestMode, bool isChinaBuild)
		{
			if (Application.isEditor)
			{
				return;
			}
			Adjust adjust = Object.FindObjectOfType<Adjust>();
			if (adjust == null)
			{
				GameObject gameObject = new GameObject("Adjust");
				gameObject.SetActive(value: false);
				adjust = gameObject.AddComponent<Adjust>();
				adjust.startManually = false;
				adjust.eventBuffering = false;
				adjust.sendInBackground = true;
				adjust.launchDeferredDeeplink = false;
				adjust.appToken = adjustAppToken;
				adjust.environment = ((!isTestMode) ? AdjustEnvironment.Production : AdjustEnvironment.Sandbox);
				adjust.logLevel = (VoodooLog.IsDebugLogsEnabled ? AdjustLogLevel.Verbose : AdjustLogLevel.Suppress);
				if (isChinaBuild)
				{
					VoodooLog.LogDebug(Module.ANALYTICS, "AdjustWrapper", "China app -> AdjustUrlStrategy is left to default");
				}
				else
				{
					VoodooLog.LogDebug(Module.ANALYTICS, "AdjustWrapper", "WorldWide app -> AdjustUrlStrategy is set to India");
					adjust.urlStrategy = AdjustUrlStrategy.India;
				}
				adjust.AttributionChange = OnAttributionChange;
				adjust.needsCost = true;
				gameObject.SetActive(value: true);
			}
			else
			{
				adjust.gameObject.name = "Adjust";
			}
		}

		internal static AttributionData GetAttributionData()
		{
			return AttributionData;
		}

		private static void OnAttributionChange(AdjustAttribution adjustAttribution)
		{
		}
	}
}
